#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //ui->record->setEnabled(false);
    ui->port->setText("8080"); // 设置默认端口号
    ui->ip->setText("127.0.0.1"); // 设置默认Ip
    printHostIPAddresses();

    setWindowTitle("客户端") ;
    ui->disconnect->setDisabled(true);

    // 实例化监听的套接字对象
    m_tcp = new QTcpSocket(this);
   
    connect(m_tcp,&QTcpSocket::readyRead,this,[=](){
        QByteArray data = m_tcp->readAll();
        ui->record->append("服务器say: " + data); // 追加进通信记录中

    });

    // 服务器断开连接
    connect(m_tcp, &QTcpSocket::disconnected,this,[=](){
        m_tcp->close();
        ui->C_status->setText("未连接");
        ui->record->append("服务器已断开连接！");

        ui->disconnect->setEnabled(false);
        ui->connect->setDisabled(false);
    });

    // 收到信号代表建立了新连接
    connect(m_tcp,&QTcpSocket::connected,this,[=](){
        ui->C_status->setText("已连接");
        QString msg = MyHost + ": 已成功连接至服务器!";
        m_tcp->write(msg.toUtf8());
        ui->record->append(msg);

        ui->disconnect->setEnabled(true);
        ui->connect->setDisabled(true);
    });

    ui->C_status->setText("未连接");
}

MainWindow::~MainWindow()
{
    delete ui;
}


//void MainWindow::on_setListen_clicked()
//{
//    unsigned short port = ui->port->text().toUShort(); // 读出端口号并转换成无符号短整型
//    m_s->listen(QHostAddress::Any, port); // 启动监听，绑定IP与端口
//    ui->setListen->setText("已启用监听");
//    ui->setListen->setEnabled(false); // 绑定后按钮设置为不可用
//}

void MainWindow::on_sendMsg_clicked()
{
    QString msg = MyHost + ": " + ui->msg->toPlainText(); // 将数据以纯文本的方式取出来
    m_tcp->write(msg.toUtf8());
    ui->record->append(msg); // 追加进通信记录中
    ui->msg->setText("");
}

void MainWindow::on_connect_clicked()
{
    QString ip = ui->ip->text();
    unsigned short port = ui->port-> text().toUShort();
    m_tcp->connectToHost(ip, port);
}

void MainWindow::on_disconnect_clicked()
{
    QString msg = MyHost + "已与服务器断开连接";// 将数据以纯文本的方式取出来
    m_tcp->write(msg.toUtf8());

    m_tcp->close();
    ui->disconnect->setEnabled(false);
    ui->connect->setDisabled(false);
    ui->record->append("已主动与服务器断开连接！");
}

// 获取主机Ipv4地址
void MainWindow::printHostIPAddresses() {
    QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
    foreach (QNetworkInterface interface, interfaces) {
        if (interface.flags().testFlag(QNetworkInterface::IsUp) && !interface.flags().testFlag(QNetworkInterface::IsLoopBack)) {
            QList<QNetworkAddressEntry> entries = interface.addressEntries();
            foreach (QNetworkAddressEntry entry, entries) {
                // 输出所有类型的IP地址
                //qDebug() << "IP Address:" << entry.ip().toString();

                // 如果你只想要IPv4地址，可以添加以下检查
                if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
                    //qDebug() << "IPv4 Address:" << entry.ip().toString();
                    MyHost = entry.ip().toString();
                }
            }
        }
    }
}
















